﻿using System;
using Slippor.Common.PluginFramework;

namespace Slippor.Douban.Radio.Filter
{
    public abstract class FilterPlugin : IPlugin
    {
        protected IApplication _application;
        protected String _description = "";
        protected String _name = "";
        protected abstract IFilter Filter { get; }

        #region IPlugin Members

        public IApplication Application
        {
            get { return _application; }
            set { _application = value; }
        }

        public string Name
        {
            get { return _name; }
            set { }
        }

        public string Description
        {
            get { return _description; }
            set { }
        }

        public void Load()
        {
            if (_application != null)
            {
                var filterService = (IFilterService) _application.GetService(typeof (IFilterService));
                if (filterService != null)
                {
                    filterService.AddFilter(Filter);
                }
                if (Loaded != null)
                {
                    Loaded(this, null);
                }
            }
        }

        public void UnLoad()
        {
            if (_application != null)
            {
                var filterService = (IFilterService) _application.GetService(typeof (IFilterService));
                if (filterService != null)
                {
                    filterService.RemoveFilter(Filter);
                }
                if (UnLoaded != null)
                {
                    UnLoaded(this, null);
                }
            }
        }

        public event EventHandler Loaded;
        public event EventHandler UnLoaded;

        #endregion
    }
}